package main

import (
	"encoding/json"
	"fmt"
	"log"
)

// JSONUtil 工具类
type JSONUtil struct{}

func (JSONUtil) toString(obj interface{}) string {
	jsonBytes, err := json.Marshal(obj)
	if err != nil {
		return ""
	}
	return string(jsonBytes)
}

// ToJsonStrIndent 格式化输出的JSON字符串
func (JSONUtil) ToJsonStrIndent(obj interface{}) string {
	jsonBytes, err := json.MarshalIndent(obj, "", "  ")
	if err != nil {
		log.Printf("JSON序列化失败: %v", err)
		return ""
	}
	return string(jsonBytes)
}

type Article struct {
	Title    string
	Content  string
	CoverImg []string
}

func main() {
	// 创建JSONUtil实例
	jsonUtil := JSONUtil{}

	// 创建示例数据
	articleVo := &Article{
		Title:    "示例文章",
		CoverImg: []string{"/sta/ss/ss.inp", "image2.jpg", "image3.jpg"},
		Content:  "这是文章内容",
	}
	fmt.Printf("toString:%+v\n", articleVo.CoverImg)
	str := jsonUtil.toString(articleVo.CoverImg)
	//反射 输出类型

	println(str)
}
